package DecertifiSys.untill.baseUntill;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhoumeineng on 2018/2/1.
 */
public class HttpDataUntill {


    public static JSONObject httpRspSet(HttpServletRequest req, HttpServletResponse rsp){
        rsp.addHeader("Access-Control-Allow-Origin", "*");

        rsp.setContentType("application/json;charset=UTF-8");
        System.out.printf("------"+req.getParameterMap().toString());
        return   new JSONObject(req.getParameter("message"));
    }

    /**
     * 发送post请求给其他的系统
     * @param jsonObject 参数
     * @param url 请求连接
     * */
    public static JSONObject PostReqsuet(JSONObject jsonObject,String url,RestTemplate restTemplate){
        HttpHeaders headers = new HttpHeaders();
        MediaType type2 = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type2);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<String> formEntity = new HttpEntity <String>(jsonObject.toString(),headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,formEntity,String.class);
        System.out.printf("++++++++++"+responseEntity.getBody());
        return new  JSONObject(responseEntity.getBody());
    }
}
